import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {ExtendHttpService} from "../../../common/extend-http.service";
import {Router} from "@angular/router";
import {TipService} from "../../services/tip.service";

@Component({
  selector: 'app-waiter-count-list',
  templateUrl: './waiter-count-list.component.html',
  styleUrls: ['./waiter-count-list.component.css']
})
export class WaiterCountListComponent implements OnInit {
  @Output() viewState = new EventEmitter();
  loading = true;
  isFlag = false;
  isFlagMore = false;
  tipType = 'error';
  totalPage = 1;
  loadMoreFlag = true;
  list = [];
  params = {startTime: '', endTime: ''};

  // 详情参数
  detailParams = {
    startTime: '',
    endTime: '',
    uid: '',
    start: 1
  };
  detailList = [];

  // 日志记录
  logList = [];

  // 支付方式
  optList = [];

  // 查看详情的参数
  isLoading = false; //  背景遮罩
  detailsShow = false; // 是否展示详情页
  details: any; // 基本信息
  odList: any; // 已点菜品
  detailsFlag = true;

  constructor(private extendHttp: ExtendHttpService,
              private router: Router,
              private tipService: TipService) {
  }

  ngOnInit() {
    // input为date的元素的特殊处理 格式为: 2018-02-25
    var nowEnd = new Date();
    var monthEnd = (nowEnd.getMonth() + 1);
    var day = nowEnd.getDate();
    var m = [];
    if (monthEnd < 10) {
      m.push(0);
      m.push(monthEnd);
    } else {
      m.push(monthEnd);
    }
    var d = []
    if (day < 10) {
      d.push(0);
      d.push(day);
    } else {
      d.push(day);
    }
    var today = nowEnd.getFullYear() + '-' + m.join('') + '-' + d.join('');
    this.params.endTime = today;

    var nowStart = new Date();
    nowStart.setMonth(nowStart.getMonth() - 1);
    var monthStart = nowStart.getMonth() + 1;
    var dayStart = nowStart.getDate();

    var m1 = [];
    var d1 = [];
    if (monthStart < 10) {
      m1.push(0);
      m1.push(monthStart);
    }
    if (dayStart < 10) {
      d1.push(0);
      d1.push(dayStart);
    } else {
      d1.push(dayStart)
    }
    var todayStart = nowEnd.getFullYear() + '-' + m1.join('') + '-' + d1.join('');
    this.params.startTime = todayStart;
    cBack = () => {
      $('#btn-back').trigger('click');
    }
    this.getData();
  }

  getData(searchFlag?: boolean) {
    if (searchFlag) {
      if (!this.params.startTime.trim()) {
        this.tipService.change(true, '请选择开始时间');
        return;
      }
      if (!this.params.endTime.trim()) {
        this.tipService.change(true, '请选择结束时间');
        return;
      }
    }

    this.extendHttp.post('/merchantApp/Statistics/PointSingleStatistics', this.params).subscribe(response => {
      let data = response.json();
      if (data.code == '0') {
        this.list = data.odList;
        this.isFlag = true;
        this.loading = false;
      } else if (data.code == '-2001') {
        this.tipType = 'error';
        this.tipService.change(true, '登录超时，跳转到登录页面');
        setTimeout(() => {
          this.router.navigate(['/login']);
        }, 2000);
      } else {
        this.tipType = 'error';
        this.loading = false;
        this.tipService.change(true, data.message);
      }
    }, error => {
      this.tipType = 'error';
      this.loading = false;
      this.tipService.change(true, '系统繁忙，操作失败！');
    });
  }
  clearData() {
    this.params.startTime = '';
    this.params.endTime = ''
  }

  getDetails(uid) {
    this.viewState.emit(uid);
  }
}
